import React, { createContext, useState, useEffect,useRef } from 'react';
import { Toast } from 'antd-mobile'
import HeadTop from '@/components/HeadTop';
import ListItem from '../_parts/ListItem';
import FooterInfinite from '@/components/FooterInfinite';
import { validatenull } from "@/utils/validate";
import ApiIndex from '@/api/handTasks';
import { listFields } from '../_parts/const';
import ChangeUserModal from '@/components/ChangeUserModal';

import { MyContext } from '@/context/index';
const { Provider, Consumer } = MyContext

function Index ({ history }) {
    const [List, setList] = useState([]); //列表
    const [hasMore, setHasMore] = useState(true) //上拉
    const [pageNum, setPageNum] = useState(1) //页码
    const childRef = useRef(null);
    const childPid = useRef(null);

    let loading = false
    const getRequest = async () => {
        if (loading) return
        loading = true
        let params = {
            page: pageNum,
            size: 10,
            type: '',
            status: '',
            repairUser: '',
            statusList: ['ACCEPTED', 'REPAIRED_TO_BE_CONFIRMED', 'CANCEL_TO_BE_CONFIRMED', 'REPAIRING']
        }

        let res = await ApiIndex.getList(params)
        if (res.data.code) {
            Toast.show({
                content: '请求失败:' + res.data.msg,
            })
            return
        }
        if (res.data.data.records.length) {

            let list = res.data.data.records
            setList(List.concat(list))
            setHasMore(true)
            setPageNum(pageNum + 1)
            loading = false
        }
        if (res.data.data.current == res.data.data.pages) {
            setHasMore(false)
            loading = true
        }
    }

    // 搜索
    const sendHandlePressVal = (value) => {
        if (validatenull(value)) return
        let _value = value
        if (value.indexOf("*") != -1) {
            _value = value.split("*")[0]
        }

        handleDebounce(_value, List)
    }
    const handleDebounce = (value, list) => {
        if (list.length < 1) {
            Toast.show({
                content: '暂无数据'
            })
            return
        }

        let arr = list.filter(el => el.no == value)
        if (!arr.length) {
            Toast.show({
                content: '暂无匹配数据'
            })
            return
        }
        arr.length && setList(arr)
    }

    const cancelRow = (row)=>{
        history.push("/equipmentDetail", { no: row.no })
    }

    const changePserson = (obj) => {
        childPid.current = obj.id
        console.log( childPid.current)
        if (childRef.current) {
            childRef.current.show();
        }
    }

    return (
        <div className='page-wrapper'>
            <Provider value={{ placeholderTxt: "请输入维修编号" }}>
                <HeadTop
                    title="受理任务"
                    history={history}
                    search={true}
                    sendHandlePressVal={sendHandlePressVal}
                ></HeadTop>
            </Provider>
            <div className="page-body" style={{ padding: 15 }}>
                {
                    List.length > 0 ?
                        List.map(el => <ListItem
                            key={el.id}
                            item={el}
                            history={history}
                            detailUrl="/handTasksDetail"
                            disabledUrlStatus="REPAIRED_TO_BE_CONFIRMED,CANCEL_TO_BE_CONFIRMED"
                            listFields={listFields}
                            cancelRow={cancelRow}
                            changePserson={ el => changePserson(el)}
                        ></ListItem>)

                        : <div className='no-list-data'>
                            无更多数据
                        </div>

                }
                <FooterInfinite getRequest={getRequest} hasMore={hasMore}></FooterInfinite>
            </div>
            <ChangeUserModal ref={childRef} isRepair={true}  pId={childPid} loadPage={getRequest}></ChangeUserModal>

        </div>
    );
}

export default Index;
